package com.cls.fins_demo.config;

import com.xiaoleilu.hutool.util.HexUtil;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

/**
 * Handler
 *
 * @author CLS
 * @since 2023/08/04
 */
public class MyClientHandler extends ChannelInboundHandlerAdapter {

    /**
     * Fins协议第一次握手
     *
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        //握手
        ByteBuf byteBuf = Unpooled.wrappedBuffer(HexUtil.encodeHexStr("46494E530000000C00000000000000000000000D").getBytes());
        ctx.writeAndFlush(byteBuf);
    }

    /**
     * 消息处理
     *
     * @param ctx
     * @param msg
     * @throws Exception
     */
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        //接收服务端发送过来的消息
        ByteBuf byteBuf = (ByteBuf) msg;
        byte[] bytes = ByteBufUtil.getBytes(byteBuf);
        System.err.println(HexUtil.encodeHexStr(bytes));

    }
}